import React, { Component } from 'react'
import ReactECharts from 'echarts-for-react';

import { Card, Button } from 'antd'


export default class Pie extends Component {

  state = {
    date: [
      { value: 1048, name: 'Search Engine' },
      { value: 735, name: 'Direct' },
      { value: 580, name: 'Email' },
      { value: 484, name: 'Union Ads' },
      { value: 300, name: 'Video Ads' }
    ]
  }

  getOption = (date) => {
    return {
      title: {
        text: 'Referer of a Website',
        subtext: 'Fake Data',
        left: 'center'
      },
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'vertical',
        left: 'left'
      },
      series: [
        {
          name: 'Access From',
          type: 'pie',
          radius: '50%',
          data: date,
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    }
  }

  update = () => {
    this.setState(state => ({
      date: state.date.map(item => item.value + 1),
    }))
  }

  render () {
    const { date } = this.state
    return (
      <div>
        <Card>
          <Button type="primary" onClick={this.update}>更新</Button>
        </Card>
        <Card title="饼图">
          <ReactECharts option={this.getOption(date)} />
        </Card>
      </div>
    )
  }
}
